package com.xsl.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @描述：生成Lombok Model的插件
 * @作者：韦
 * @时间：2019/3/31
 */
public class LombokPlugin extends PluginAdapter {

    private Map<String, String> fixedValues;

    public LombokPlugin() {
        fixedValues = new HashMap<>();
        fixedValues.put("id", "主键");
        fixedValues.put("insertTime", "数据添加时间");
        fixedValues.put("insertMan", "数据添加人");
        fixedValues.put("modifyTime", "最后一次数据修改时间");
        fixedValues.put("modifyMan", "最后一次修改人");
        fixedValues.put("invalid", "已失效数据：0 否；1 是");
    }

    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        if (interfaze != null) {
            interfaze.addJavaDocLine("/**");
            interfaze.addJavaDocLine(" * 作者： Wbq");
            interfaze.addJavaDocLine(" * 时间： " + getToday());
            interfaze.addJavaDocLine(" * 描述： " + introspectedTable.getFullyQualifiedTableNameAtRuntime());
            interfaze.addJavaDocLine(" */");


            interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper"));
            interfaze.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Component"));
            interfaze.addAnnotation("@Mapper");
            interfaze.addAnnotation("@Component");
        }

        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        //添加domain的import
        topLevelClass.addImportedType("lombok.Data");
        topLevelClass.addImportedType("lombok.Builder");
        topLevelClass.addImportedType("lombok.NoArgsConstructor");
        topLevelClass.addImportedType("lombok.AllArgsConstructor");

        //添加domain的注解
        topLevelClass.addAnnotation("@Data");
        topLevelClass.addAnnotation("@Builder");
        topLevelClass.addAnnotation("@NoArgsConstructor");
        topLevelClass.addAnnotation("@AllArgsConstructor");

        //添加domain的注释
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(" * 作者： Wbq");
        topLevelClass.addJavaDocLine(" * 时间： " + getToday());
        topLevelClass.addJavaDocLine(" * 描述： " + introspectedTable.getFullyQualifiedTableNameAtRuntime());
        topLevelClass.addJavaDocLine(" */");

        return true;
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {

        if (introspectedColumn.getRemarks() == null || introspectedColumn.getRemarks().length() == 0) {
            String s = fixedValues.get(field.getName());
            if (s != null) {
                field.addJavaDocLine("/* " + s + " */");
            } else {
                field.addJavaDocLine("/*  */");
            }
        } else {
            field.addJavaDocLine("/* " + introspectedColumn.getRemarks() + " */");
        }

        return true;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return false;
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return false;
    }

    private String getToday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        return sdf.format(new Date());
    }
}
